package service;

import dataAcsessObject.Exception.NotEntityFoundException;
import dataAcsessObject.JPADAOFactory;
import entity.Node;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import service.exception.ObjectNotFoundException;
import valueObject.CourseVO;
import valueObject.NodeVO;

/**
 *
 * @author Juan Hernández Serrato
 */
public class NodeService extends AbstractService<NodeVO, Node> {

    @Override
    public void create(NodeVO valueObject, EntityManager em) {
        JPADAOFactory.getInstance().getNodeDAO().create(valueObject.toEntity(), em);
    }

    @Override
    public void update(NodeVO valueObject, EntityManager em) throws ObjectNotFoundException {
        try {
            Node node = JPADAOFactory.getInstance().getNodeDAO().findById(valueObject.getId(), em);
            node = valueObject.toEntity();
            JPADAOFactory.getInstance().getNodeDAO().update(node, em);
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public void delete(Object id, EntityManager em) throws ObjectNotFoundException {
        try {
            Node node = JPADAOFactory.getInstance().getNodeDAO().findById(id, em);
            JPADAOFactory.getInstance().getNodeDAO().delete(node, em);
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public NodeVO findById(Object id, EntityManager em) throws ObjectNotFoundException {
        try {
            return JPADAOFactory.getInstance().getNodeDAO().findById(id, em).toValueObject();
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public List<NodeVO> findAll(EntityManager em) {
        List<NodeVO> nodeVOs = new ArrayList<NodeVO>();
        List<Node> nodes = JPADAOFactory.getInstance().getNodeDAO().findAll(em);
        for (Node node : nodes) {
            nodeVOs.add(node.toValueObject());
        }
        return nodeVOs;
    }

    @Override
    public List<NodeVO> findRange(int begin, int end, EntityManager em) {
        List<NodeVO> nodeVOs = new ArrayList<NodeVO>();
        List<Node> nodes = JPADAOFactory.getInstance().getNodeDAO().findRange(begin, end, em);
        for (Node node : nodes) {
            nodeVOs.add(node.toValueObject());
        }
        return nodeVOs;
    }
    
    public List<NodeVO> findNodesByParent(NodeVO nodeVO, EntityManager em) {
        List<NodeVO> nodeVOs = new ArrayList<NodeVO>();
        List<Node> nodes = JPADAOFactory.getInstance().getNodeDAO().findNodesByParent(nodeVO.getId(), em);
        for (Node node : nodes) {
            nodeVOs.add(node.toValueObject());
        }
        return nodeVOs;
    }
    
    public List<NodeVO> findNodesByCourse(CourseVO courseVO, EntityManager em) {
        List<NodeVO> nodeVOs = new ArrayList<NodeVO>();
        List<Node> nodes = JPADAOFactory.getInstance().getNodeDAO().findNodesByCourse(courseVO.getId(), em);
        for (Node node : nodes) {
            nodeVOs.add(node.toValueObject());
        }
        return nodeVOs;
    }

}
